<span class='l l-1 source c'><s class='comment block c' data-entity-scope='comment.block.c'><s class='punctuation definition comment c' data-entity-scope='punctuation.definition.comment.c'>/*</s> stringmerge.c -- Given two sorted files of strings, it creates
</span><span class='l l-2 source c'> *            a sorted file consisting of all their elements.
</span><span class='l l-3 source c'> *            The names of the files are passed as command
</span><span class='l l-4 source c'> *            line parameters.
</span><span class='l l-5 source c'> <s class='punctuation definition comment c' data-entity-scope='punctuation.definition.comment.c'>*/</s></s>
</span><span class='l l-6 source c'>
</span><span class='l l-7 source c'><s class='meta preprocessor c include' data-entity-scope='meta.preprocessor.c.include'>#<s class='keyword control import include c' data-entity-scope='keyword.control.import.include.c'>include</s> <s class='string quoted other lt-gt include c' data-entity-scope='string.quoted.other.lt-gt.include.c'><s class='punctuation definition string begin c' data-entity-scope='punctuation.definition.string.begin.c'>&lt;</s>stdio.h<s class='punctuation definition string end c' data-entity-scope='punctuation.definition.string.end.c'>&gt;</s></s></s>
</span><span class='l l-8 source c'><s class='meta preprocessor macro c' data-entity-scope='meta.preprocessor.macro.c'>#define <s class='keyword control import define c' data-entity-scope='keyword.control.import.define.c'>MAXBUFFER</s> <s class='constant numeric c' data-entity-scope='constant.numeric.c'>128</s></s>
</span><span class='l l-9 source c'>
</span><span class='l l-10 source c'><s class='storage type c' data-entity-scope='storage.type.c'>int</s><s class='meta function c' data-entity-scope='meta.function.c'><s class='punctuation whitespace function leading c' data-entity-scope='punctuation.whitespace.function.leading.c'> </s><s class='entity name function c' data-entity-scope='entity.name.function.c'>getline</s><s class='meta parens c' data-entity-scope='meta.parens.c'>(FILE * fd, <s class='storage type c' data-entity-scope='storage.type.c'>char</s> buff[], <s class='storage type c' data-entity-scope='storage.type.c'>int</s> nmax)</s><s class='meta block c' data-entity-scope='meta.block.c'>{
</span><span class='l l-11 source c'>  <s class='comment block c' data-entity-scope='comment.block.c'><s class='punctuation definition comment c' data-entity-scope='punctuation.definition.comment.c'>/*</s> It reads a line from fd and stores up to nmax of
</span><span class='l l-12 source c'>   * its characters to buff.
</span><span class='l l-13 source c'>   <s class='punctuation definition comment c' data-entity-scope='punctuation.definition.comment.c'>*/</s></s>
</span><span class='l l-14 source c'>  <s class='storage type c' data-entity-scope='storage.type.c'>char</s> c;
</span><span class='l l-15 source c'>  <s class='storage type c' data-entity-scope='storage.type.c'>int</s> n=<s class='constant numeric c' data-entity-scope='constant.numeric.c'>0</s>;
</span><span class='l l-16 source c'>
</span><span class='l l-17 source c'>  <s class='keyword control c' data-entity-scope='keyword.control.c'>while</s><s class='meta initialization c' data-entity-scope='meta.initialization.c'> <s class='punctuation definition parameters c' data-entity-scope='punctuation.definition.parameters.c'>(</s></s>(c=<s class='support function C99 c' data-entity-scope='support.function.C99.c'>getc</s>(fd))!=&#39;\n&#39;)<s class='meta block c' data-entity-scope='meta.block.c'>{
</span><span class='l l-18 source c'>    <s class='keyword control c' data-entity-scope='keyword.control.c'>if</s>(c==EOF)<s class='keyword control c' data-entity-scope='keyword.control.c'>return</s> EOF;
</span><span class='l l-19 source c'>    <s class='keyword control c' data-entity-scope='keyword.control.c'>if</s>(n&lt;nmax)
</span><span class='l l-20 source c'>      buff[n++]=c;
</span><span class='l l-21 source c'>  }</s>
</span><span class='l l-22 source c'>  buff[n]=&#39;\<s class='constant numeric c' data-entity-scope='constant.numeric.c'>0</s>&#39;;
</span><span class='l l-23 source c'>  <s class='keyword control c' data-entity-scope='keyword.control.c'>return</s> n;
</span><span class='l l-24 source c'>}</s></s>
</span><span class='l l-25 source c'>
</span><span class='l l-26 source c'><s class='storage type c' data-entity-scope='storage.type.c'>int</s><s class='meta function c' data-entity-scope='meta.function.c'><s class='punctuation whitespace function leading c' data-entity-scope='punctuation.whitespace.function.leading.c'> </s><s class='entity name function c' data-entity-scope='entity.name.function.c'>stringMerge</s><s class='meta parens c' data-entity-scope='meta.parens.c'>(<s class='storage type c' data-entity-scope='storage.type.c'>char</s> filename1[], <s class='storage type c' data-entity-scope='storage.type.c'>char</s> filename2[] , <s class='storage type c' data-entity-scope='storage.type.c'>char</s> filename3[])</s> <s class='meta block c' data-entity-scope='meta.block.c'>{
</span><span class='l l-27 source c'>  <s class='comment block c' data-entity-scope='comment.block.c'><s class='punctuation definition comment c' data-entity-scope='punctuation.definition.comment.c'>/*</s> Given two sorted files of strings, called filename1 and filename2,
</span><span class='l l-28 source c'>   * it writes their merged sequence to the file filename3.
</span><span class='l l-29 source c'>   * It returns the total number of strings written to filename3.
</span><span class='l l-30 source c'>   <s class='punctuation definition comment c' data-entity-scope='punctuation.definition.comment.c'>*/</s></s>
</span><span class='l l-31 source c'>  FILE *fd1, *fd2, *fd3;
</span><span class='l l-32 source c'>  <s class='storage type c' data-entity-scope='storage.type.c'>char</s> buffer1[MAXBUFFER], buffer2[MAXBUFFER];
</span><span class='l l-33 source c'>  <s class='storage type c' data-entity-scope='storage.type.c'>int</s> ln1, ln2;
</span><span class='l l-34 source c'>  <s class='storage type c' data-entity-scope='storage.type.c'>int</s> n=<s class='constant numeric c' data-entity-scope='constant.numeric.c'>0</s>;
</span><span class='l l-35 source c'>
</span><span class='l l-36 source c'>  <s class='keyword control c' data-entity-scope='keyword.control.c'>if</s><s class='meta initialization c' data-entity-scope='meta.initialization.c'> <s class='punctuation definition parameters c' data-entity-scope='punctuation.definition.parameters.c'>(</s></s>(fd1=<s class='support function C99 c' data-entity-scope='support.function.C99.c'>fopen</s>(filename1, <s class='string quoted double c' data-entity-scope='string.quoted.double.c'><s class='punctuation definition string begin c' data-entity-scope='punctuation.definition.string.begin.c'>&quot;</s>r<s class='punctuation definition string end c' data-entity-scope='punctuation.definition.string.end.c'>&quot;</s></s>))==<s class='constant language c' data-entity-scope='constant.language.c'>NULL</s>) <s class='meta block c' data-entity-scope='meta.block.c'>{
</span><span class='l l-37 source c'><s class='punctuation whitespace support function leading c' data-entity-scope='punctuation.whitespace.support.function.leading.c'>    </s><s class='support function C99 c' data-entity-scope='support.function.C99.c'>perror</s>(<s class='string quoted double c' data-entity-scope='string.quoted.double.c'><s class='punctuation definition string begin c' data-entity-scope='punctuation.definition.string.begin.c'>&quot;</s>fopen<s class='punctuation definition string end c' data-entity-scope='punctuation.definition.string.end.c'>&quot;</s></s>);
</span><span class='l l-38 source c'><s class='punctuation whitespace support function leading c' data-entity-scope='punctuation.whitespace.support.function.leading.c'>    </s><s class='support function C99 c' data-entity-scope='support.function.C99.c'>exit</s>(<s class='constant numeric c' data-entity-scope='constant.numeric.c'>1</s>);
</span><span class='l l-39 source c'>  }</s>
</span><span class='l l-40 source c'>  <s class='keyword control c' data-entity-scope='keyword.control.c'>if</s><s class='meta initialization c' data-entity-scope='meta.initialization.c'> <s class='punctuation definition parameters c' data-entity-scope='punctuation.definition.parameters.c'>(</s></s>(fd2=<s class='support function C99 c' data-entity-scope='support.function.C99.c'>fopen</s>(filename2, <s class='string quoted double c' data-entity-scope='string.quoted.double.c'><s class='punctuation definition string begin c' data-entity-scope='punctuation.definition.string.begin.c'>&quot;</s>r<s class='punctuation definition string end c' data-entity-scope='punctuation.definition.string.end.c'>&quot;</s></s>))==<s class='constant language c' data-entity-scope='constant.language.c'>NULL</s>) <s class='meta block c' data-entity-scope='meta.block.c'>{
</span><span class='l l-41 source c'><s class='punctuation whitespace support function leading c' data-entity-scope='punctuation.whitespace.support.function.leading.c'>    </s><s class='support function C99 c' data-entity-scope='support.function.C99.c'>perror</s>(<s class='string quoted double c' data-entity-scope='string.quoted.double.c'><s class='punctuation definition string begin c' data-entity-scope='punctuation.definition.string.begin.c'>&quot;</s>fopen<s class='punctuation definition string end c' data-entity-scope='punctuation.definition.string.end.c'>&quot;</s></s>);
</span><span class='l l-42 source c'><s class='punctuation whitespace support function leading c' data-entity-scope='punctuation.whitespace.support.function.leading.c'>    </s><s class='support function C99 c' data-entity-scope='support.function.C99.c'>exit</s>(<s class='constant numeric c' data-entity-scope='constant.numeric.c'>1</s>);
</span><span class='l l-43 source c'>  }</s>
</span><span class='l l-44 source c'>  <s class='keyword control c' data-entity-scope='keyword.control.c'>if</s><s class='meta initialization c' data-entity-scope='meta.initialization.c'> <s class='punctuation definition parameters c' data-entity-scope='punctuation.definition.parameters.c'>(</s></s>(fd3=<s class='support function C99 c' data-entity-scope='support.function.C99.c'>fopen</s>(filename3, <s class='string quoted double c' data-entity-scope='string.quoted.double.c'><s class='punctuation definition string begin c' data-entity-scope='punctuation.definition.string.begin.c'>&quot;</s>w<s class='punctuation definition string end c' data-entity-scope='punctuation.definition.string.end.c'>&quot;</s></s>))==<s class='constant language c' data-entity-scope='constant.language.c'>NULL</s>) <s class='meta block c' data-entity-scope='meta.block.c'>{
</span><span class='l l-45 source c'><s class='punctuation whitespace support function leading c' data-entity-scope='punctuation.whitespace.support.function.leading.c'>    </s><s class='support function C99 c' data-entity-scope='support.function.C99.c'>perror</s>(<s class='string quoted double c' data-entity-scope='string.quoted.double.c'><s class='punctuation definition string begin c' data-entity-scope='punctuation.definition.string.begin.c'>&quot;</s>fopen<s class='punctuation definition string end c' data-entity-scope='punctuation.definition.string.end.c'>&quot;</s></s>);
</span><span class='l l-46 source c'><s class='punctuation whitespace support function leading c' data-entity-scope='punctuation.whitespace.support.function.leading.c'>    </s><s class='support function C99 c' data-entity-scope='support.function.C99.c'>exit</s>(<s class='constant numeric c' data-entity-scope='constant.numeric.c'>1</s>);
</span><span class='l l-47 source c'>  }</s>
</span><span class='l l-48 source c'>
</span><span class='l l-49 source c'>  ln1 =<s class='meta function-call c' data-entity-scope='meta.function-call.c'><s class='punctuation whitespace function-call leading c' data-entity-scope='punctuation.whitespace.function-call.leading.c'> </s><s class='support function any-method c' data-entity-scope='support.function.any-method.c'>getline</s>(</s>fd1,buffer1,MAXBUFFER-<s class='constant numeric c' data-entity-scope='constant.numeric.c'>1</s>);
</span><span class='l l-50 source c'>  ln2 =<s class='meta function-call c' data-entity-scope='meta.function-call.c'><s class='punctuation whitespace function-call leading c' data-entity-scope='punctuation.whitespace.function-call.leading.c'> </s><s class='support function any-method c' data-entity-scope='support.function.any-method.c'>getline</s>(</s>fd2,buffer2,MAXBUFFER-<s class='constant numeric c' data-entity-scope='constant.numeric.c'>1</s>);
</span><span class='l l-51 source c'>
</span><span class='l l-52 source c'>  <s class='keyword control c' data-entity-scope='keyword.control.c'>while</s><s class='meta initialization c' data-entity-scope='meta.initialization.c'> <s class='punctuation definition parameters c' data-entity-scope='punctuation.definition.parameters.c'>(</s></s>(ln1!=EOF) &amp;&amp; (ln2!=EOF))<s class='meta block c' data-entity-scope='meta.block.c'>{
</span><span class='l l-53 source c'>    <s class='keyword control c' data-entity-scope='keyword.control.c'>if</s><s class='meta initialization c' data-entity-scope='meta.initialization.c'> <s class='punctuation definition parameters c' data-entity-scope='punctuation.definition.parameters.c'>(</s></s><s class='support function C99 c' data-entity-scope='support.function.C99.c'>strcmp</s>(buffer1,buffer2)&lt;=<s class='constant numeric c' data-entity-scope='constant.numeric.c'>0</s>)<s class='meta block c' data-entity-scope='meta.block.c'>{
</span><span class='l l-54 source c'><s class='punctuation whitespace support function leading c' data-entity-scope='punctuation.whitespace.support.function.leading.c'>      </s><s class='support function C99 c' data-entity-scope='support.function.C99.c'>fprintf</s>(fd3, <s class='string quoted double c' data-entity-scope='string.quoted.double.c'><s class='punctuation definition string begin c' data-entity-scope='punctuation.definition.string.begin.c'>&quot;</s><s class='constant other placeholder c' data-entity-scope='constant.other.placeholder.c'>%s</s><s class='constant character escape c' data-entity-scope='constant.character.escape.c'>\n</s><s class='punctuation definition string end c' data-entity-scope='punctuation.definition.string.end.c'>&quot;</s></s>, buffer1);
</span><span class='l l-55 source c'>      ln1 =<s class='meta function-call c' data-entity-scope='meta.function-call.c'><s class='punctuation whitespace function-call leading c' data-entity-scope='punctuation.whitespace.function-call.leading.c'> </s><s class='support function any-method c' data-entity-scope='support.function.any-method.c'>getline</s>(</s>fd1,buffer1,MAXBUFFER-<s class='constant numeric c' data-entity-scope='constant.numeric.c'>1</s>);
</span><span class='l l-56 source c'>    }</s><s class='keyword control c' data-entity-scope='keyword.control.c'>else</s><s class='meta block c' data-entity-scope='meta.block.c'>{
</span><span class='l l-57 source c'><s class='punctuation whitespace support function leading c' data-entity-scope='punctuation.whitespace.support.function.leading.c'>      </s><s class='support function C99 c' data-entity-scope='support.function.C99.c'>fprintf</s>(fd3, <s class='string quoted double c' data-entity-scope='string.quoted.double.c'><s class='punctuation definition string begin c' data-entity-scope='punctuation.definition.string.begin.c'>&quot;</s><s class='constant other placeholder c' data-entity-scope='constant.other.placeholder.c'>%s</s><s class='constant character escape c' data-entity-scope='constant.character.escape.c'>\n</s><s class='punctuation definition string end c' data-entity-scope='punctuation.definition.string.end.c'>&quot;</s></s>, buffer2);
</span><span class='l l-58 source c'>      ln2 =<s class='meta function-call c' data-entity-scope='meta.function-call.c'><s class='punctuation whitespace function-call leading c' data-entity-scope='punctuation.whitespace.function-call.leading.c'> </s><s class='support function any-method c' data-entity-scope='support.function.any-method.c'>getline</s>(</s>fd2,buffer2,MAXBUFFER-<s class='constant numeric c' data-entity-scope='constant.numeric.c'>1</s>);
</span><span class='l l-59 source c'>    }</s>
</span><span class='l l-60 source c'>    n++;
</span><span class='l l-61 source c'>  }</s>
</span><span class='l l-62 source c'>
</span><span class='l l-63 source c'>  <s class='keyword control c' data-entity-scope='keyword.control.c'>while</s><s class='meta initialization c' data-entity-scope='meta.initialization.c'> <s class='punctuation definition parameters c' data-entity-scope='punctuation.definition.parameters.c'>(</s></s>ln1!=EOF)<s class='meta block c' data-entity-scope='meta.block.c'>{
</span><span class='l l-64 source c'><s class='punctuation whitespace support function leading c' data-entity-scope='punctuation.whitespace.support.function.leading.c'>      </s><s class='support function C99 c' data-entity-scope='support.function.C99.c'>fprintf</s>(fd3, <s class='string quoted double c' data-entity-scope='string.quoted.double.c'><s class='punctuation definition string begin c' data-entity-scope='punctuation.definition.string.begin.c'>&quot;</s><s class='constant other placeholder c' data-entity-scope='constant.other.placeholder.c'>%s</s><s class='constant character escape c' data-entity-scope='constant.character.escape.c'>\n</s><s class='punctuation definition string end c' data-entity-scope='punctuation.definition.string.end.c'>&quot;</s></s>, buffer1);
</span><span class='l l-65 source c'>      ln1=<s class='meta function-call c' data-entity-scope='meta.function-call.c'><s class='support function any-method c' data-entity-scope='support.function.any-method.c'>getline</s>(</s>fd1,buffer1,MAXBUFFER-<s class='constant numeric c' data-entity-scope='constant.numeric.c'>1</s>);
</span><span class='l l-66 source c'>      n++;
</span><span class='l l-67 source c'>  }</s>
</span><span class='l l-68 source c'>
</span><span class='l l-69 source c'>  <s class='keyword control c' data-entity-scope='keyword.control.c'>while</s><s class='meta initialization c' data-entity-scope='meta.initialization.c'> <s class='punctuation definition parameters c' data-entity-scope='punctuation.definition.parameters.c'>(</s></s>ln2!=EOF)<s class='meta block c' data-entity-scope='meta.block.c'>{
</span><span class='l l-70 source c'><s class='punctuation whitespace support function leading c' data-entity-scope='punctuation.whitespace.support.function.leading.c'>      </s><s class='support function C99 c' data-entity-scope='support.function.C99.c'>fprintf</s>(fd3, <s class='string quoted double c' data-entity-scope='string.quoted.double.c'><s class='punctuation definition string begin c' data-entity-scope='punctuation.definition.string.begin.c'>&quot;</s><s class='constant other placeholder c' data-entity-scope='constant.other.placeholder.c'>%s</s><s class='constant character escape c' data-entity-scope='constant.character.escape.c'>\n</s><s class='punctuation definition string end c' data-entity-scope='punctuation.definition.string.end.c'>&quot;</s></s>, buffer2);
</span><span class='l l-71 source c'>      ln2=<s class='meta function-call c' data-entity-scope='meta.function-call.c'><s class='support function any-method c' data-entity-scope='support.function.any-method.c'>getline</s>(</s>fd2,buffer2,MAXBUFFER-<s class='constant numeric c' data-entity-scope='constant.numeric.c'>1</s>);
</span><span class='l l-72 source c'>      n++;
</span><span class='l l-73 source c'>  }</s>
</span><span class='l l-74 source c'>
</span><span class='l l-75 source c'><s class='punctuation whitespace support function leading c' data-entity-scope='punctuation.whitespace.support.function.leading.c'>  </s><s class='support function C99 c' data-entity-scope='support.function.C99.c'>fclose</s>(fd1);
</span><span class='l l-76 source c'><s class='punctuation whitespace support function leading c' data-entity-scope='punctuation.whitespace.support.function.leading.c'>  </s><s class='support function C99 c' data-entity-scope='support.function.C99.c'>fclose</s>(fd2);
</span><span class='l l-77 source c'><s class='punctuation whitespace support function leading c' data-entity-scope='punctuation.whitespace.support.function.leading.c'>  </s><s class='support function C99 c' data-entity-scope='support.function.C99.c'>fclose</s>(fd3);
</span><span class='l l-78 source c'>  <s class='keyword control c' data-entity-scope='keyword.control.c'>return</s> n;
</span><span class='l l-79 source c'>}</s></s>
</span><span class='l l-80 source c'>
</span><span class='l l-81 source c'><s class='storage type c' data-entity-scope='storage.type.c'>int</s><s class='meta function c' data-entity-scope='meta.function.c'><s class='punctuation whitespace function leading c' data-entity-scope='punctuation.whitespace.function.leading.c'> </s><s class='entity name function c' data-entity-scope='entity.name.function.c'>main</s><s class='meta parens c' data-entity-scope='meta.parens.c'>(<s class='storage type c' data-entity-scope='storage.type.c'>int</s> argc, <s class='storage type c' data-entity-scope='storage.type.c'>char</s> *argv[])</s> <s class='meta block c' data-entity-scope='meta.block.c'>{
</span><span class='l l-82 source c'>  <s class='keyword control c' data-entity-scope='keyword.control.c'>if</s>(argc!=<s class='constant numeric c' data-entity-scope='constant.numeric.c'>4</s>)<s class='meta block c' data-entity-scope='meta.block.c'>{
</span><span class='l l-83 source c'><s class='punctuation whitespace support function leading c' data-entity-scope='punctuation.whitespace.support.function.leading.c'>    </s><s class='support function C99 c' data-entity-scope='support.function.C99.c'>printf</s>(<s class='string quoted double c' data-entity-scope='string.quoted.double.c'><s class='punctuation definition string begin c' data-entity-scope='punctuation.definition.string.begin.c'>&quot;</s>Usage: <s class='constant other placeholder c' data-entity-scope='constant.other.placeholder.c'>%s so</s>rtedfile1 sortedfile2 mergefile<s class='constant character escape c' data-entity-scope='constant.character.escape.c'>\n</s><s class='punctuation definition string end c' data-entity-scope='punctuation.definition.string.end.c'>&quot;</s></s>, argv[<s class='constant numeric c' data-entity-scope='constant.numeric.c'>0</s>]);
</span><span class='l l-84 source c'><s class='punctuation whitespace support function leading c' data-entity-scope='punctuation.whitespace.support.function.leading.c'>    </s><s class='support function C99 c' data-entity-scope='support.function.C99.c'>exit</s>(<s class='constant numeric c' data-entity-scope='constant.numeric.c'>0</s>);
</span><span class='l l-85 source c'>  }</s>
</span><span class='l l-86 source c'><s class='punctuation whitespace support function leading c' data-entity-scope='punctuation.whitespace.support.function.leading.c'>  </s><s class='support function C99 c' data-entity-scope='support.function.C99.c'>printf</s>(<s class='string quoted double c' data-entity-scope='string.quoted.double.c'><s class='punctuation definition string begin c' data-entity-scope='punctuation.definition.string.begin.c'>&quot;</s>We have <s class='constant other placeholder c' data-entity-scope='constant.other.placeholder.c'>%d</s> merged records<s class='constant character escape c' data-entity-scope='constant.character.escape.c'>\n</s><s class='punctuation definition string end c' data-entity-scope='punctuation.definition.string.end.c'>&quot;</s></s>,
</span><span class='l l-87 source c'><s class='meta function-call c' data-entity-scope='meta.function-call.c'><s class='punctuation whitespace function-call leading c' data-entity-scope='punctuation.whitespace.function-call.leading.c'>   </s><s class='support function any-method c' data-entity-scope='support.function.any-method.c'>stringMerge</s>(</s>argv[<s class='constant numeric c' data-entity-scope='constant.numeric.c'>1</s>], argv[<s class='constant numeric c' data-entity-scope='constant.numeric.c'>2</s>], argv[<s class='constant numeric c' data-entity-scope='constant.numeric.c'>3</s>]));
</span><span class='l l-88 source c'>}</s></s></span>
